package com.ruoyi.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.domain.Market;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.SaleRecordMapper;
import com.ruoyi.domain.SaleRecord;
import com.ruoyi.service.ISaleRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 出售记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@Service
public class SaleRecordServiceImpl  extends ServiceImpl<SaleRecordMapper, SaleRecord> implements ISaleRecordService
{
    @Autowired
    private SaleRecordMapper saleRecordMapper;

    /**
     * 查询出售记录
     *
     * @param id 出售记录主键
     * @return 出售记录
     */
    @Override
    public SaleRecord selectSaleRecordById(Long id)
    {
        return saleRecordMapper.selectSaleRecordById(id);
    }

    /**
     * 查询出售记录列表
     *
     * @param saleRecord 出售记录
     * @return 出售记录
     */
    @Override
    public List<SaleRecord> selectSaleRecordList(SaleRecord saleRecord)
    {
        return saleRecordMapper.selectSaleRecordList(saleRecord);
    }

    /**
     * 新增出售记录
     *
     * @param saleRecord 出售记录
     * @return 结果
     */
    @Override
    public int insertSaleRecord(SaleRecord saleRecord)
    {
        saleRecord.setRecordSerial(UUID.fastUUID().toString());//流水号
        saleRecord.setCreateTime(DateUtils.getNowDate());
        saleRecord.setCreateBy(ShiroUtils.getLoginName());
        return saleRecordMapper.insertSaleRecord(saleRecord);
    }

    /**
     * 修改出售记录
     *
     * @param saleRecord 出售记录
     * @return 结果
     */
    @Override
    public int updateSaleRecord(SaleRecord saleRecord)
    {
        return saleRecordMapper.updateSaleRecord(saleRecord);
    }

    /**
     * 批量删除出售记录
     *
     * @param ids 需要删除的出售记录主键
     * @return 结果
     */
    @Override
    public int deleteSaleRecordByIds(String ids)
    {
        return saleRecordMapper.deleteSaleRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除出售记录信息
     *
     * @param id 出售记录主键
     * @return 结果
     */
    @Override
    public int deleteSaleRecordById(Long id)
    {
        return saleRecordMapper.deleteSaleRecordById(id);
    }

    /**
     * 可视化页面功能1
     * @return
     */
    @Override
    public List<Market> keshihuaList1() {
        List<Market> markets=saleRecordMapper.keshihuaList1();//品牌占比
        double count= markets.stream().mapToDouble(Market::getCommodityPriceShow).sum();
        for (int i = 0; i < markets.size(); i++) {
            Market market=markets.get(i);
            DecimalFormat decimalFormat = new DecimalFormat("#.##");
            String formattedNumber = (decimalFormat.format(market.getCommodityPriceShow()*100/count))+"%";
            market.setBrandProportion(formattedNumber);
        }
        return markets;
    }
    /**
     * 可视化页面功能2
     * @return
     */
    @Override
    public List<Market> keshihuaList2() {
        List<Market> markets=saleRecordMapper.keshihuaList2();//商品类型占比
        double count= markets.stream().mapToDouble(Market::getCommodityPriceShow).sum();
        for (int i = 0; i < markets.size(); i++) {
            Market market=markets.get(i);
            market.setCount(String.valueOf(count));
        }

        return markets;
    }

    @Override
    public List<Market> keshihuaList3() {
        List<Market> markets=new ArrayList<>();//销售
        markets.add(saleRecordMapper.keshihuaList31());//当日销售金额
        markets.add(saleRecordMapper.keshihuaList32());//本周销售金额
        markets.add(saleRecordMapper.keshihuaList33());//本月销售金额
        markets.add(saleRecordMapper.keshihuaList34());//本年销售金额
        markets.add(saleRecordMapper.keshihuaList35());//总销售金额
        return markets;
    }

    @Override
    public List<Market> shujushaixuanList3() {
        return saleRecordMapper.shujushaixuanList3();
    }

    @Override
    public List<Market> shujushaixuanList4() {
        return saleRecordMapper.shujushaixuanList4();
    }

    @Override
    public List<Market> shujushaixuanList5() {
        return saleRecordMapper.shujushaixuanList5();
    }

    @Override
    public List<Market> shujushaixuanList6() {
        return saleRecordMapper.shujushaixuanList6();
    }

    @Override
    public List<Market> shujushaixuanList7() {
        return saleRecordMapper.shujushaixuanList7();
    }

    @Override
    public List<Market> shujushaixuanList8() {
        return saleRecordMapper.shujushaixuanList8();
    }

    public static void main(String[] args) {
        double s=111;
        double q=333;
        double c=s/q;
        DecimalFormat decimalFormat = new DecimalFormat("#.##");

        String formattedNumber = decimalFormat.format(c);
        System.out.println(formattedNumber);
    }
}
